<?php

namespace App\Console\Commands;

use App\Http\Model\CrossRoomPkRecord;
use App\Http\Model\Union;
use App\Http\Services\RoomServices;
use Carbon\Carbon;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Redis;

class CrossRoomPkPool extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'CreateCrossRoomPkPool';

    protected $crossRoomPkService;//PK服务层
    protected $unionModel;//公会model
    protected $roomService;

    protected $crossRoomPkModel = 'cross_room_pk_record';//pk记录表
    protected $unionRoomTable = 'union_room';
    protected $roomMicroLogTable = 'room_micro_log';//上麦记录
    protected $roomOpenTimeTable = 'room_open_time';//房间时长

    protected $crossRoomPkRedisList = 'CROSS:ROOM:PK:ALLIST'; //pk房间池

    protected $log = 'cross_room_pk';//日志
    protected $error_log = 'error_cross_room_pk';
    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '更新跨房PK随机池(房内上麦累计满后台配置人数 且 开播>30分钟的房间 )';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
        $this->crossRoomPkService = new CrossRoomPkRecord();
        $this->unionModel = new Union();
        $this->roomService = new RoomServices();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {  
        $todayEnd = Carbon::today()->endOfDay()->toDateTimeString();
        $todayStart = Carbon::today()->startOfDay()->toDateTimeString();

        //获取后台配置PK信息
        $setInfo = $this->crossRoomPkService->getPkMinutes();
        do{
            //查看所有CBD房间
            $unionList = $this->unionModel->getAllUnion();
            $unionList = array_column($unionList,'id');
            $rooms = DB::table($this->unionRoomTable)->whereIn('un_id', $unionList)
                    ->leftJoin("room", "room.room_id", "=", "union_room.room_id")
                    ->where('room.business_type',1)
                    ->select('room.*')
                    ->get();
            $rooms = objToArray($rooms);
            $roomIds = array_column($rooms,'room_id');
            $anchorList = []; 
            if(!empty($roomIds)){
                foreach($roomIds as $r){
                    //房间上麦记录
                    $micList = $this->roomService->microUserList(['room_id'=>$r]);
                    if($micList['code'] == 0){
                        //上麦人
                        $anchorList[$r] = array_column($micList['data']['general_micro_list'],'user_id');
                    }
                }
            }
            //上麦人数>=5的房间
            $upMicPeople = [];
            foreach($roomIds as $r){
                if( isset($anchorList[$r]) && count($anchorList[$r]) >= $setInfo['room_people']){
                    $upMicPeople[] = $r;
                }
            }
            if(empty($upMicPeople)){
                write_log($this->log,'INFO 无房间',$upMicPeople);
                break;
            }
            //删除不满足房间
            $allPoolData = Redis::SMEMBERS($this->crossRoomPkRedisList);
            $diffId = array_diff($allPoolData,$upMicPeople);
            if(!empty($diffId)){
                Redis::SREM($this->crossRoomPkRedisList,$diffId);
            }
            //房间开播时长
            $roomOpenNumTrue = DB::table($this->roomOpenTimeTable)
            ->whereIn('room_id',$upMicPeople)
            ->where('total_time', '>=', $setInfo['room_open_time'])
            ->where('created_time','>=',$todayStart)
            ->where('created_time','<=',$todayEnd)
            ->get();
            $roomOpenNumTrue = objToArray($roomOpenNumTrue);
            
            $poolIds = array_column($roomOpenNumTrue,'room_id');
            if(empty($poolIds)){
                write_log($this->log,'INFO 无房间',$poolIds);
                break;
            }
            //redis存储PK列表
            Redis::SADD($this->crossRoomPkRedisList,$poolIds);
        }while(false);

    }
}